package com.liholin.springboot.sample.controller;

import com.liholin.springboot.sample.bean.PostInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("config")
@Slf4j
public class ConfigController {

    @Value("${book.name}")
    private String bookName;
    @Value("${book.author}")
    private String bookAuthor;
    @Value("${book.desc}")
    private String bookDesc;

    @GetMapping("conBook")
    private String conBook() {
        log.info("bookName:{},bookAuthor:{},bookDesc:{}", bookName, bookAuthor, bookDesc);
        return "bookName:" + bookName + "\nbookAuthor:" + bookAuthor + "\nbookDesc:" + bookDesc;
    }

    @Value("${blog.value}")
    private String blogValue;
    @Value("${blog.number}")
    private Integer blogNumber;
    @Value("${blog.bignumber}")
    private Long blogBigNumber;
    @Value("${blog.test1}")
    private Integer blogTest1;
    @Value("${blog.test2}")
    private Integer blogTest2;

    @GetMapping("random")
    private String getRandom() {
        log.info("blogValue:{},blogNumber:{},blogBigNumber:{},blogTest1:{},blogTest2:{}", blogValue, blogNumber, blogBigNumber, blogTest1, blogTest2);
        return "blogValue:" + blogValue + "\nblogNumber:" + blogNumber + "\nblogBigNumber:" + blogBigNumber + "\nblogTest1:" + blogTest1
                + "\nblogTest2:" + blogTest2;
    }

    @Autowired
    private Environment env;

    @GetMapping("bind")
    private String bind() {
        Binder binder = Binder.get(env);
        FooProperties fooProperties = binder.bind("com.mszlu", Bindable.of(FooProperties.class)).get();
        log.info("foot:{}", fooProperties.getFoo());

        List<String> post = binder.bind("com.mszlu.post", Bindable.listOf(String.class)).get();
        System.out.println(post);
        List<PostInfo> postInfos = binder.bind("com.mszlu.posts", Bindable.listOf(PostInfo.class)).get();
        System.out.println(postInfos);
        return "foot:" + fooProperties.getFoo() + "\npost：" + post.toString() + "\nposts:" + postInfos.toString();
    }
}
